package com.sms.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetTaskObject extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		int level = Integer.parseInt(request.getParameter("level"));
		int proID = Integer.parseInt(request.getParameter("proID"));
		
		List<Student> list = new ArrayList<Student>();
		if(level == 2) {
			list = User.getAllStudents(proID);
		} else {
			list = User.getLeaderStudent(proID);
		}
		
		if (list != null && list.size() > 0) {
			JSONArray json = new JSONArray();
			List jlist = new ArrayList();
			for (int i = 0; i < list.size(); i++) {
				Student s = (Student) list.get(i);
				String sname = s.getSname();
				JSONObject j = new JSONObject();
				j.put("sno", s.getSno());
				j.put("sname", sname);
				json.add(j);
			}
			out.println(json);
		} else {
			System.out.println("没有学生可选择");
		}
		
		out.flush();
		out.close();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}


}
